package com.hua.yuan.live;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.hua.yuan.base.BaseAppActivity;
import com.hua.yuan.base.Constents;
import com.hua.yuan.dialog.ConfirmCancelDialog;
import com.hua.yuan.live.adapter.LiveReservationAdapter;
import com.hua.yuan.live.listener.OnBuyClickListener;
import com.hua.yuan.live.repertory.BusinessFactory;
import com.hua.yuan.live.repertory.business.IBusiness;
import com.hua.yuan.live.repertory.cloud_data.loader.LiveReservationLoader;
import com.hua.yuan.live.repertory.cloud_data.loader.LiveGetReservationLoader;
import com.hua.yuan.live.repertory.cloud_data.res.ResercationBean;
import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;

/**
 * 时间：2018/8/22
 * 作者：蓝天
 * 描述：预约视频
 */
public class LiveReservationActivity extends BaseAppActivity implements View.OnClickListener,
        LoaderManager.LoaderCallbacks<List<ResercationBean>>,
        OnBuyClickListener, ConfirmCancelDialog.OnConfirmListener,
        LiveReservationLoader.onReservationCallback {
    private ImageView backImage;
    private TextView titleView;
    private TextView tv_anchor_name;
    private ImageView iv_anchor_cover;
    private ListView mVideoListView;
    private LiveReservationAdapter mAdapter;
    private LiveGetReservationLoader mReservationLoader;
    public final static int TASK_ID = 1004;
    private ConfirmCancelDialog mConfigDialog;
    private int mPosition;
    private int money;
    private String mToken;
    private int mAnchorId;
    private String rtcUserId;
    private String mNickName;
    private ResercationBean mCurrentBean;


    @Override
    protected int initLayoutId() {
        return R.layout.activity_live_video;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void initView() {
        //TODO 此处缺少主播的头像和主播的名称
        mAdapter = new LiveReservationAdapter(this, this);
        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);
        tv_anchor_name = findViewById(R.id.tv_anchor_name);
        iv_anchor_cover = findViewById(R.id.iv_anchor_cover);
        mVideoListView = findViewById(R.id.lv_videolist);
        mVideoListView.setAdapter(mAdapter);
        mToken = PreferenceUtils.getString(Constents.TOKEN);
        mAnchorId = getIntent().getIntExtra(LiveModule.INTENT_ANCHOR_ID, 0);
        mNickName=getIntent().getStringExtra(LiveModule.INTENT_ANCHOR_NICK_NAME);
        tv_anchor_name.setText(mNickName);
        mReservationLoader = (LiveGetReservationLoader) getSupportLoaderManager().initLoader(TASK_ID, null, this);
        mReservationLoader.requestData(mToken, mAnchorId);
        mReservationLoader.forceLoad();
        mConfigDialog = ConfirmCancelDialog.create(getSupportFragmentManager());
        mConfigDialog.setOnConfirmListener(this);
        money = getIntent().getIntExtra(LiveModule.INTENT_USER_MONEY, 0);
        rtcUserId=getIntent().getStringExtra(LiveModule.INTENT_ANCHOR_RTC_USER_ID);
    }

    @Override
    protected void initNet() {

//                backImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });
//        titleView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ConfirmCancelDialog.create(getSupportFragmentManager()).show();
//            }
//        });
////        tv_anchor_name.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View view) {
//////                Intent intent = new Intent(LiveVideoActivity.this, LiveVideoP2PActivity.class);
//////                startActivity(intent);
////                Intent intent = new Intent(LiveVideoActivity.this, RTCDoubleRoomActivity.class);
////                startActivity(intent);
////            }
////        });
//
//        List<String> list = new ArrayList<>();
//        list.add("");
//        list.add("");
//        list.add("");
//        list.add("");
//        list.add("");
//        lv_videolist.setAdapter(new ListAdapter(list));

    }


    @Override
    public void onClick(View v) {

    }

    @NonNull
    @Override
    public Loader<List<ResercationBean>> onCreateLoader(int id, @Nullable Bundle args) {
        return new LiveGetReservationLoader(this);
    }

    @Override
    public void onLoadFinished(@NonNull Loader<List<ResercationBean>> loader, List<ResercationBean> data) {

        if (data == null) {
            return;
        }
        mAdapter.setData(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoaderReset(@NonNull Loader<List<ResercationBean>> loader) {

    }

    @Override
    public void click(int position) {

        mPosition = position;
        IBusiness<Integer,ResercationBean> business=BusinessFactory.getInstance().getBusiness(1);
        ResercationBean resercationBean= (ResercationBean) mAdapter.getData().get(position);
        Integer onClickType=business.execute(resercationBean);
        mCurrentBean=resercationBean;
        //如果可以预约，则弹出预约信息
        switch (onClickType){
            case 0:
                break;
            case 1:
                //可点击
                showDialog(resercationBean);
                break;
            case 2:
                jumpP2pActivity();
                break;
        }
    }

    private void showDialog(ResercationBean resercationBean){
        if(money>resercationBean.getMoney()){
            LiveReservationLoader.reservation(mToken, mAnchorId, resercationBean.getStartTime(), resercationBean.getEndTime(), String.valueOf(resercationBean.getMoney()), this);
            return;
        }
        mConfigDialog.setMoney(String.valueOf(resercationBean.getMoney()),getApplicationContext());
        mConfigDialog.show();
    }

    @Override
    public void onConfirm(View v) {
        Intent intent = new Intent();
        intent.setClassName(this, "com.hua.yuan.ui.MoneyBagActivity");
        intent.putExtra("buyMoney",String.valueOf(mCurrentBean.getMoney()));
        intent.putExtra("movieId",mAnchorId);
        intent.putExtra("isTuji",false);
        if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            startActivity(intent);
            return;
        }
    }

    @Override
    public void resevation(Integer isSuccess) {

        if (isSuccess == 0) {
            //连接成功
            Toast.makeText(getApplicationContext(), "预约成功", Toast.LENGTH_SHORT).show();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ResercationBean resercationBean = (ResercationBean) mAdapter.getData().get(mPosition);
                    if(resercationBean!=null){
                        resercationBean.setIsUserAppointment(1);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            });

        } else {
            Toast.makeText(getApplicationContext(), "预约失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void jumpP2pActivity(){
        Intent intent = new Intent();
        intent.setClassName(this, "com.hua.yuan.live.LiveVideoPhoneActivity");
        intent.putExtra(LiveModule.INTENT_ANCHOR_RTC_USER_ID,rtcUserId);
        if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            startActivity(intent);
        }
        return;
    }
}
